package controllers;


import controllers.images.LoadImage;
import dto.Language;
import dto.RelatedTopicDto;
import dto.TopicDto;
import models.MyByteArrayBinder;
import models.MyCustomStringBinder;
import models.Topic;
import models.test.Product;
import org.apache.commons.io.IOUtils;
import play.data.Upload;
import play.data.binding.As;
import play.data.binding.types.ByteArrayBinder;
import play.data.validation.Required;
import play.i18n.Lang;
import play.i18n.Messages;
import play.mvc.Controller;
import play.mvc.With;
import services.TopicService;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.logging.Logger;

@With(Secure.class)
public class Admin extends Controller{
    static Logger logger = Logger.getLogger(Admin.class.getName());
    public static void products() {
        List<Product> products = Product.all().fetch();
        render(products);
    }

    public static byte[] getSampleImage() {
        InputStream is = LoadImage.class.getResourceAsStream("coating_main.jpg");

        byte[] bytes = new byte[0];
        try {
            bytes = IOUtils.toByteArray(is);
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }

        return bytes;
    }

    public static void addProd() {
        render();
    }


    public static void getPicture(long id) {
        Product p = Product.findById(id);
        response.setContentTypeIfNotSet("jpg");
        if (p.image!=null) {
            renderBinary(new ByteArrayInputStream(p.image));
        }
    }

    public static void doAddProd(String name, String code) {
        System.out.print("asdadasd:" + name + "|" + code);

        logger.info("asdadasd:" + name + "|" + code);
        
        Product p = new Product();
        p.code = code; 
        p.name = name;

        p.image = getSampleImage();

        System.out.println("Length="+p.image.length);

        p.save();
        
        redirect("Admin.products");
    }

    public static void inputTopic(Long topicId) {
        Topic topic = (topicId == null) ? new Topic() : (Topic) Topic.findById(topicId);
        TopicDto topicDto = TopicService.createTopicDto(topic);

        List<RelatedTopicDto> allRelatedTopics = TopicService.getAllRelatedTopicDto();
        
        Language[] allLanguages = Language.all;

        render(topicDto, allRelatedTopics, allLanguages);
    }

    public static void listTopic() {
        List<Topic> topics = Topic.all().fetch();
        List<TopicDto> topicDtos = TopicService.createTopicDtos(topics);
        render(topicDtos);
    }
    
    public static void saveTopic(TopicDto topicDto) {
        if (topicDto.topicId==null) {
            if (TopicService.isExisted(topicDto.code, topicDto.language)) {
                String err = "Da ton tai topic voi code=" + topicDto.code + ", language=" + topicDto.language;
                flash.error(err);
                renderTemplate("Admin/error.html");
            }
        }

        TopicService.mergeTopic(topicDto);
        redirect("Admin.listTopic");
    }

    public static void deleteTopic(Long id) {
        Topic t = new Topic(id);
        t.delete();
        redirect("Admin.listTopic");
    }

    /**
     *
     * @param topicId
     * @param type  : 1 = relatedPhoto, 2 = largePhoto
     */
    public static void getTopicImage(Long topicId, int type) {
        redirect("User.getTopicImage", topicId, type);
    }

    public static void index() {
        render();
    }
    
    public static void changeLanguage(String lang) {
        Lang.change(lang);
        redirect("Admin.index");
    }
}
